﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using E_Commerce.Domain.Interfaces;
using Dapper;
using System.Data;
using E_Commerce.Domain.Entities;

namespace E_Commerce.Infrastructure.Data
{
    public class DapperRepository<T> : IRepository<T> where T : class
    {
        //protected受保护的
        protected string ConnStr = "";
        //依赖注入
        protected IDbConnection Conn;
        
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int id)
        {
            return await Conn.DeleteAsync<T>(id);
        }
        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetByIdAsync(int id)
        {
            return await Conn.GetAsync<T>(id);
        }
        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="whereCondition"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T>> GetListAsync(string whereCondition = "", object paras = null)
        {
            return await Conn.GetListAsync<T>(whereCondition,paras);
        }
        /// <summary>
        /// 分页+条件查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="whereCondition">总条数</param>
        /// <param name="paras">商品名称</param>
        /// <returns></returns>
        public async Task<CommandPageModel<T>> GetPageListAsync(int pageIndex, int pageSize, string whereCondition = "", object paras = null)
        {
            //返回总条数
           int totalCount = await Conn.RecordCountAsync<T>(whereCondition, paras);
            //分页结果集
           var data= await Conn.GetListPagedAsync<T>(pageIndex, pageSize, whereCondition, "", paras);
            //实例化
            CommandPageModel<T> pageModel = new CommandPageModel<T>();
            pageModel.Data = data;
            pageModel.TotalCount = totalCount;
            return pageModel;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(T obj)
        {
            return await Conn.InsertAsync<T>(obj) ?? -1;//??语法的意思是：当前面表示式为null的时候，返回？？后面的值;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> ShowAsync()
        {
            return (List<T>)await Conn.GetListAsync<T>();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T obj)
        {
            return await Conn.UpdateAsync<T>(obj);
        }
    }
}
